import { defineConfig } from "vite";
import * as path from 'path'
import uni from "@dcloudio/vite-plugin-uni";
import AutoImport from 'unplugin-auto-import/vite'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [uni(),
  AutoImport({
    // targets to transform 目标转换
    include: [/\.[tj]sx?$/, /\.vue$/, /\.vue\?vue/, /\.md$/],

    // global imports to register
    imports: ['vue', 'vue-router', 'pinia', 'uni-app'], // 插件预设支持导入的api 自定义导入的api
    // Generate corresponding .eslintrc-auto-import.json file. 生成对应的.eslintrc-auto-import.json文件。
    // eslint globals Docs - https://eslint.org/docs/user-guide/configuring/language-options#specifying-globals
    eslintrc: {
      enabled: false, // Default `false`
      filepath: './.eslintrc-auto-import.json', // Default `./.eslintrc-auto-import.json`
      globalsPropValue: true // Default `true`, (true | false | 'readonly' | 'readable' | 'writable' | 'writeable')
    },
    dirs: ['src/utils', 'src/api/modules', 'src/utils/dataType', 'src/utils/entity/'],
    // Filepath to generate corresponding .d.ts file.
    // Defaults to './auto-imports.d.ts' when `typescript` is installed locally.
    // Set `false` to disable.
    dts: 'src/types/auto-imports.d.ts'
  })
  ],
  build: {
    target: 'es2015', //指定es版本,浏览器的兼容性 最低 es2015
    outDir: 'dist', //指定打包输出路径
    // assetsDir: 'assets', //指定静态资源存放路径
    cssCodeSplit: true, //css代码拆分,禁用则所有样式保存在一个css里面
    sourcemap: false, //是否构建source map 文件

    //minify: 'terser', // 默认是esbuild，需要配置为terser
    // 生产环境取消 console
    terserOptions: {
      compress: {
        drop_console: true,
        drop_debugger: true
      }
    },
    //会打包出 css js 等文件夹 目录显得清晰
    rollupOptions: {
      output: {
        chunkFileNames: "js/[name]-[hash].js",
        entryFileNames: "js/[name]-[hash].js",
        assetFileNames: "[ext]/[name]-[hash].[ext]",
      },
    },
  },
  server: {
    // host: "cs.cqyukexing.com", // 指定服务器主机名
    port: 8686, // 指定服务端口号
    // open: false, // 运行自动打开浏览器
    // https: true, // 关闭https
    // strictPort: true, // 若8888端口被占用,直接结束项目
    proxy: {
      '/api': {
        target: 'http://192.168.88.26:8088',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api/, '')
      },
      '/Appraise': {
        target: 'http://192.168.88.26:8088',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/Appraise/, '')
      },
      '/AliOssSts': {
        target: 'https://上传文件域名',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/AliOssSts/, '')
      }
    }
  },
  resolve: {
    // 路径别名配置
    alias: {
      '@': path.resolve(__dirname, 'src'),
      '@img': path.resolve(__dirname, 'src/static/image')
    }
  }
});
